import  _ from 'lodash'
import Tab from '@/editor/js/tab/tab'
import store from '@/store/index'
export default class TabList {
    constructor(options){
      var errMsg;
      // check whether a custom Tab type is set
      // if (_.has(options, 'tabModel')) {
      //   this.TabModel = _.get(options, 'tabModel');
      //   // check whether the custom type is of type Tab
      //   if (!this.TabModel instanceof Tab) {
      //     errMsg = 'custom tab model is not a sub type of Tab: ' + Tab;
      //     throw errMsg;
      //   }
      // } else {
      //   this.TabModel = Tab;getters.app
      // }

      this._tabs = store.getters.ediEdgeTabs;
      this.options = options;
    }


    render = function () {
        var self = this;
    };

    hideTabComponents = function () {
        var tabHeaderContainer = this._$parent_el.children(_.get(this.options, 'headers.container'));
        tabHeaderContainer.hide();
        // Hide the tool palette
        //_.get(this.options, 'toolPalette').hideToolPalette();
    };

    showTabComponents = function () {
        var tabHeaderContainer = this._$parent_el.children(_.get(this.options, 'headers.container'));
        tabHeaderContainer.show();
        // Show the tool palette
        //_.get(this.options, 'toolPalette').showToolPalette();
    };

    createHeaderForTab = function(tab){

    };

    getTabContainer = function(){
        return this._$tab_container;
    };
    /**
     * add a tab to the tab list.
     *
     * @param {Tab} tab an object of tab
     * @fires TabList#tab-added
     */
    addTab = function (tab) {
        tab.setParent(this);
        this._tabs.push(tab);
        store.dispatch('siddhi/addTab',tab)
    };
    /**
     * gets tab
     * @param {string} tab id
     * @returns {*}
     */
    getTab = function (tabId) {
        return _.find(this._tabs, ['id', tabId]);
    };
    /**
     * removes a tab
     * @param {Tab} tab the tab instance
     * @fires TabList#tab-removed
     */
    removeTab = function (tab) {
        var self = this;
        var tabIndex = this._tabs.findIndex((item)=>{return item.name == tab.name})
        //switch to tab at last or next index
        //make sure there are remaining tabs
        if(this._tabs.length > 0 && !_.isEqual(tabIndex, -1)){
            // if removing tab is 0th tab, next tab is also the 0th
            let nextTabIndex = 0;
            if(_.isEqual(tabIndex, 0) && this._tabs.length>1){
                nextTabIndex = tabIndex +1;
            }else if((_.isEqual(tabIndex, 0) && this._tabs.length==1)){
              nextTabIndex = -1;
            }else{
              nextTabIndex = tabIndex -1;
            }

            var nextTab = this._tabs[nextTabIndex];
            if(nextTab != undefined){
              this.setActiveTab(nextTab);
            }
        }
      store.dispatch('siddhi/removeTab',tab)
    };
    /**
     * set selected tab
     * @param {Tab} tab the tab instance
     * @fires TabList#active-tab-changed
     */
    setActiveTab = function (tab) {
        if (!_.isEqual(this.activeTab, tab)) {
            if(!_.includes(this._tabs, tab)) {
                var errMsg = 'tab : ' + tab.cid + 'is not part of this tab list.';
                throw errMsg;
            }
            var lastActiveTab = this.activeTab;
            this.activeTab = tab;
            this.activeTab.setActive(true);
            store.dispatch('siddhi/setActiveTab',tab)
        }
    };
    /**
     * active tab
     * @returns {Tab}
     */
    getActiveTab = function () {
        return store.getters.activeTab;
    };
    getTabList = function() {
        return this._tabs;
    };
    /**
     * Creates a new tab.
     * @param opts
     *          switchToNewTab: indicate whether to switch to new tab after creation
     *          tabOptions: constructor args for the tab
     * @returns {Tab} created tab instance
     * @event TabList#tab-added
     * @fires TabList#active-tab-changed
     */
    newTab = function (opts) {
        var newTab=new Tab(opts);
        this.addTab(newTab);
        this.setActiveTab(newTab);
        newTab.id=opts.id
        return newTab;
    };

    forEach = function(callback){
        this._tabs.forEach(callback);
    }

 }
